package com.bjpowernode.service.impl;

import com.bjpowernode.entity.TTran;
import com.bjpowernode.entity.TTranHistory;
import com.bjpowernode.mapper.TTranHistoryMapper;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.query.TranHistoryQuery;
import com.bjpowernode.service.TranHistoryService;
import com.bjpowernode.util.LoginInfoUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class TranHistoryServiceImpl implements TranHistoryService {

    private final TTranHistoryMapper tTranHistoryMapper;

    private final TTranMapper tTranMapper;

    @Transactional
    @Override
    public boolean saveTranStage(TranHistoryQuery tranHistoryQuery) {

        //向交易历史记录中添加1条数据
        TTranHistory tTranHistory = new TTranHistory();
        BeanUtils.copyProperties(tranHistoryQuery, tTranHistory);
        tTranHistory.setCreateBy(LoginInfoUtil.getCurrentLoginUser().getId());
        tTranHistory.setCreateTime(new Date());
        int insert = tTranHistoryMapper.insertSelective(tTranHistory);


        //更新交易表中的数据
        TTran tTran = new TTran();
        tTran.setId(tranHistoryQuery.getTranId());
        tTran.setStage(tranHistoryQuery.getStage());
        tTran.setEditBy(LoginInfoUtil.getCurrentLoginUser().getId());
        tTran.setEditTime(new Date());

        int update = tTranMapper.updateByPrimaryKeySelective(tTran);
        return insert > 0 && update > 0;
    }
}
